/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "rtthread.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
#define CLIP(x,n,m)     (x) = (x) > (m) ? (m) : (x) < (n) ? (n) : (x)

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define DI_5_Pin GPIO_PIN_2
#define DI_5_GPIO_Port GPIOE
#define DI_4_Pin GPIO_PIN_3
#define DI_4_GPIO_Port GPIOE
#define DI_3_Pin GPIO_PIN_4
#define DI_3_GPIO_Port GPIOE
#define DI_2_Pin GPIO_PIN_5
#define DI_2_GPIO_Port GPIOE
#define DI_2_EXTI_IRQn EXTI9_5_IRQn
#define DI_1_Pin GPIO_PIN_6
#define DI_1_GPIO_Port GPIOE
#define DI_1_EXTI_IRQn EXTI9_5_IRQn
#define CTRL_5V_Pin GPIO_PIN_5
#define CTRL_5V_GPIO_Port GPIOA
#define DO_8_Pin GPIO_PIN_7
#define DO_8_GPIO_Port GPIOE
#define DO_9_Pin GPIO_PIN_8
#define DO_9_GPIO_Port GPIOE
#define DO_10_Pin GPIO_PIN_9
#define DO_10_GPIO_Port GPIOE
#define DO_11_Pin GPIO_PIN_10
#define DO_11_GPIO_Port GPIOE
#define DO_12_Pin GPIO_PIN_11
#define DO_12_GPIO_Port GPIOE
#define DO_13_Pin GPIO_PIN_12
#define DO_13_GPIO_Port GPIOE
#define DO_14_Pin GPIO_PIN_13
#define DO_14_GPIO_Port GPIOE
#define PHY_RST_Pin GPIO_PIN_10
#define PHY_RST_GPIO_Port GPIOB
#define LED_1_Pin GPIO_PIN_8
#define LED_1_GPIO_Port GPIOD
#define LED_2_Pin GPIO_PIN_9
#define LED_2_GPIO_Port GPIOD
#define RS485_EN_Pin GPIO_PIN_8
#define RS485_EN_GPIO_Port GPIOC
#define VER_1_Pin GPIO_PIN_8
#define VER_1_GPIO_Port GPIOA
#define VER_2_Pin GPIO_PIN_9
#define VER_2_GPIO_Port GPIOA
#define VER_3_Pin GPIO_PIN_10
#define VER_3_GPIO_Port GPIOA
#define DO_1_Pin GPIO_PIN_0
#define DO_1_GPIO_Port GPIOD
#define DO_2_Pin GPIO_PIN_1
#define DO_2_GPIO_Port GPIOD
#define DO_3_Pin GPIO_PIN_2
#define DO_3_GPIO_Port GPIOD
#define DO_4_Pin GPIO_PIN_3
#define DO_4_GPIO_Port GPIOD
#define DO_5_Pin GPIO_PIN_4
#define DO_5_GPIO_Port GPIOD
#define DO_6_Pin GPIO_PIN_5
#define DO_6_GPIO_Port GPIOD
#define DO_7_Pin GPIO_PIN_6
#define DO_7_GPIO_Port GPIOD
#define DI_8_Pin GPIO_PIN_7
#define DI_8_GPIO_Port GPIOD
#define DI_7_Pin GPIO_PIN_0
#define DI_7_GPIO_Port GPIOE
#define DI_6_Pin GPIO_PIN_1
#define DI_6_GPIO_Port GPIOE

/* USER CODE BEGIN Private defines */
/* thread manage */

#define XD_THREAD_PRIORITY                       8
#define XD_THREAD_STACK_SIZE                     1024
#define XD_THREAD_TIME_SLICE                     5

#define CAN_THREAD_PRIORITY                      (XD_THREAD_PRIORITY-1)
#define CAN_THREAD_STACK_SIZE                    1024
#define CAN_THREAD_TIME_SLICE                    5

#define USBD_THREAD_PRIORITY                     (XD_THREAD_PRIORITY-3)
#define USBD_THREAD_STACK_SIZE                   2048
#define USBD_THREAD_TIME_SLICE                   5

#define FFBW_THREAD_PRIORITY                     (XD_THREAD_PRIORITY+2)
#define FFBW_THREAD_STACK_SIZE                   1024
#define FFBW_THREAD_TIME_SLICE                   5

#define MOTOR_THREAD_PRIO                        (XD_THREAD_PRIORITY+1)
#define MOTOR_THREAD_STACK                       1024
#define MOTOR_THREAD_TIMESLICE                   5

#define MOTOR_HOME_THREAD_PRIO                   (XD_THREAD_PRIORITY-2)
#define MOTOR_HOME_THREAD_STACK                  1024
#define MOTOR_HOME_THREAD_TIMESLICE              1

#define CANOPEN_MOTOR_THREAD_PRIO                (XD_THREAD_PRIORITY+3)
#define CANOPEN_MOTOR_THREAD_STACK               1024
#define CANOPEN_MOTOR_THREAD_TIMESLICE           5

#define CANOPEN_TIME_THREAD_PRIORITY             (XD_THREAD_PRIORITY+3)
#define CANOPEN_TIME_THREAD_STACK_SIZE           256
#define CANOPEN_TIME_THREAD_TIME_SLICE           5

#define STRCMD(s)               {s,sizeof(#s)-1,(uint8_t*)#s,s##_Process}
struct strcmd
{
    size_t num;
    size_t len;
    uint8_t* str;
    void (*process)(uint8_t* srcStr,const struct strcmd* cmd);
};

typedef union
{
    uint8_t byte;
    struct
    {
        uint8_t bit0:1;
        uint8_t bit1:1;
        uint8_t bit2:1;
        uint8_t bit3:1;
        uint8_t bit4:1;
        uint8_t bit5:1;
        uint8_t bit6:1;
        uint8_t bit7:1;
    }bits;
}byte_bit_t;

typedef struct
{
    GPIO_TypeDef* port;
    uint16_t pin;
}port_pin_t;
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
